C Programming
Q31.
C program is given below: #include < stdio.h > int main () { int i, j; char a [2] [3] = {{'a', 'b', 'c'}, {'d', 'e', 'f'}}; char b [3] [2]; char *p = *b; for (i = 0; i < 2; i++) { for (j = 0; j < 3; j++) { *(p + 2*j + i) = a [i] [j]; } } } What should be the contents of the array b at the end of the program?Q32.
Consider the C program given below : #include < stdio.h > int main () { int sum = 0, maxsum = 0, i, n = 6; int a [] = {2, -2, -1, 3, 4, 2}; for (i = 0; i < n; i++) { if (i == 0 || a [i] < 0 || a [i] < a [i - 1]) { if (sum > maxsum) maxsum = sum; sum = (a [i] > 0) ? a [i] : 0; } else sum += a [i]; } if (sum > maxsum) maxsum = sum ; printf ("%d\n", maxsum); } What is the value printed out when this program is executed?Q33.
Consider the following C program that attempts to locate an element x in an array Y[] using binary search. The program is erroneous. 1. f(int Y[10], int x) { 2. int i, j, k; 3. i = 0; j = 9; 4. do { 5. k = (i + j) /2; 6. if( Y[k] < x) i = k; else j = k; 7. } while(Y[k] != x && i < j); 8. if(Y[k] == x) printf ("x is in the array ") ; 9. else printf (" x is not in the array ") ; 10. } The correction needed in the program to make it work properly isQ34.
Consider the C program given below. What does it print? #include < stdio.h > int main () { int i, j; int a [8] = {1, 2, 3, 4, 5, 6, 7, 8}; for(i = 0; i < 3; i++) { a[i] = a[i] + 1; i++; } i--; for (j = 7; j > 4; j--) { int i = j/2; a[i] = a[i] - 1; } printf ("%d, %d", i, a[i]);Q35.
Consider the following C program that attempts to locate an element x in an array Y[] using binary search. The program is erroneous. 1. f(int Y[10], int x) { 2. int i, j, k; 3. i = 0; j = 9; 4. do { 5. k = (i + j) /2; 6. if( Y[k] < x) i = k; else j = k; 7. } while(Y[k] != x && i < j); 8. if(Y[k] == x) printf ("x is in the array ") ; 9. else printf (" x is not in the array ") ; 10. } On which of the following contents of Y and x does the program fail?Q36.
Which one of the choices given below would be printed when the following program is executed? #include < stdio.h > int a1[] = {6, 7, 8, 18, 34, 67}; int a2[] = {23, 56, 28, 29}; int a3[] = {-12, 27, -31}; int *x[] = {a1, a2, a3}; void print(int *a[]) { printf("%d,", a[0][2]); printf("%d,", *a[2]); printf("%d,", *++a[0]); printf("%d,", *(++a)[0]); printf("%d\n", a[-1][+1]); } main() { print(x); }Q37.
Which one of the choices given below would be printed when the following program is executed ? #include < stdio.h > struct test { int i; char *c; }st[] = {5, "become", 4, "better", 6, "jungle", 8, "ancestor", 7, "brother"}; main () { struct test *p = st; p += 1; ++p -> c; printf("%s,", p++ -> c); printf("%c,", *++p -> c); printf("%d,", p[0].i); printf("%s \n", p -> c); }Q38.
Consider the following ANSI C code segment: z=x + 3 + y-> f1 + y-> f2; for (i = 0; i < 200; i = i + 2) { if (z > i) { p = p + x + 3; q = q + y-> f1; } else { p = p + y-> f2; q = q + x + 3; } }Assume that the variable y points to a struct (allocated on the heap) containing two fields f1 and f2, and the local variables x, y, z, p, q, and i are allotted registers. Common sub-expression elimination (CSE) optimization is applied on the code. The number of addition and the dereference operations (of the form y -> f1 or y -> f2) in the optimized code, respectively, are:Q39.
Consider the following ANSI C program. #include < stdio.h > int main( ) { int arr[4][5]; int i, j; for (i=0; i < 4; i++) { for (j=0; j < 5; j++) { arr[i][j] = 10 * i + j; } } printf("%d", *(arr[1]+9)); return 0; } What is the output of the above program?Q40.
Consider the following C program which is supposed to compute the transpose of a given 4 \times 4 matrix M. Note that, there is an X in the program which indicates some missing statements. Choose the correct option to replace X in the program. #include < stdio.h > #define ROW 4 #define COL 4 int M[ROW][COL] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}; main() { int i, j, t; for (i = 0; i < 4; ++i) { X } for (i = 0; i < 4; ++i) for (j = 0; j < 4; ++j) printf ("%d", M[i][j]); }